/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mut_jaeryo.circletimer;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.PacMap;

public class MainAbility extends Ability {
    private CircleTimer timer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        timer = (CircleTimer) findComponentById(ResourceTable.Id_main_timer);
        timer.setMaximumTime(3600);  // 60 minute
        timer.setInitPosition(2000);
        findComponentById(ResourceTable.Id_button_start).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                timer.start();
            }
        });

        findComponentById(ResourceTable.Id_button_stop).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                timer.stop();
            }
        });
        {

            timer.setBaseTimerEndedListener(new CircleTimer.baseTimerEndedListener() {
                @Override
                public void OnEnded() {
                    ToastDialog t = new ToastDialog(MainAbility.this);
                    t.setText("timer End");
                    t.setDuration(100000);
                    t.show();
                }
            });
            //Timer 가 종료 되었을 때 호출 된다.
        }
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        timer.onSaveInstanceState(outState);
        super.onSaveAbilityState(outState);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        timer.onRestoreInstanceState(inState);
        super.onRestoreAbilityState(inState);
    }
}
